// pages/index/index.js

var config = require('../../config');
var app=getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isshow: 'block',
    load: false,
    array1: ['东区','西区',' 南区',' 北区',' 中北'],
    objectArray1: [
      {
        id: 0,
        name: '东区'
      },
      {
        id: 1,
        name: '西区'
      },
      {
        id: 2,
        name: '南区'
      },
      {
        id: 3,
        name: '东区'
      },
      {
        id: 4,
        name: '中北'
      }
    ],
    array2: ['生活', '学习', '娱乐'],
    objectArray2: [
      {
        id: 0,
        name: '生活'
      },
      {
        id: 1,
        name: '学习'
      },
      {
        id: 2,
        name: '娱乐'
      }
    ],
    books: [],
    modalHidden: true,
    index:-1,
    index2:-1
  },

  bc1: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    });
    var _this = this;
    wx.request({
      url: `${config.service.host}/weapp/SelectBook`,
      data: {
        type: 1,
        index:_this.data.index,
        index2:_this.data.index2,
      },
      method: "GET",
      header: {
        "Content-Type": "application/json"
      },
      success: function (e) {
        _this.setData({ books: e.data })
        console.log(e.data);
      }
    })

  },

  bc2: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)

    this.setData({
      index2: e.detail.value
    });
    var _this = this;
    wx.request({
      url: `${config.service.host}/weapp/SelectBook`,
      data: {
        type: 1,
        index: _this.data.index,
        index2: _this.data.index2,
      },
      method: "GET",
      header: {
        "Content-Type": "application/json"
      },
      success: function (e) {
        _this.setData({ books: e.data })
        console.log(e.data);
      }
    })

  },

  bindViewTap: function (e) {
		var name = e.target.id;
		var books = new Array();
		var contectway="";
    var contect="";
		books=this.data.books;
		for(var i=0;i<books.length;i++){
			if(name==books[i].sellername){
				contectway=books[i].contectId;
				contect=books[i].contect;
			}
		}
		wx.showModal({
			title: '联系方式',
			content: contectway+":"+contect,
			success: function (res) {
				if (res.confirm) {
					console.log('用户点击确定')
				} else if (res.cancel) {
					console.log('用户点击取消')
				}
			}
		})  

  },

  modalBindaconfirm: function () {
    this.setData({
      modalHidden: !this.data.modalHidden
    })
  },

  modalBindcancel: function () {
    this.setData({
      modalHidden: !this.data.modalHidden

     })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              console.log(res.userInfo);
              getApp().globalData.userInfo = res.userInfo;
              getApp().globalData.logged=true;
            }
          })
          _this.setData({ isshow: 'none' })
        }else{
          _this.setData({ isshow: 'block' })
        }
      }
    })

    
  },

  bindGetUserInfo: function (e) {
    console.log(e.detail.userInfo);
    this.setData({ isshow: 'none' })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this=this;
    wx.request({
      url: `${config.service.host}/weapp/Books`,
      data: {
        type: 1
      },
      method: "GET",
      header: {
        "Content-Type": "application/json"
      },
      success: function (e) {
        _this.setData({ books: e.data })
        console.log(e.data);
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  search: function () {
    wx.navigateTo({
      url: 'search',
    })
  },


})

